package common

import "fmt"

func main() {
	nums := []int{1, 2, 3}
	list := ArrayToList(nums)
	PrintList(list)
	PrintList(list)
}

type ListNode struct {
	Val  int
	Next *ListNode
}

/**
 * @Description: 将数组转换为链表
 * @param nums 数组
 * @return *ListNode 链表
 */
func ArrayToList(nums []int) *ListNode {
	length := len(nums)
	if length == 0 {
		return nil
	}
	head := &ListNode{Val: nums[0]}
	curr := head
	for i := 1; i < length; i++ {
		curr.Next = &ListNode{Val: nums[i]}
		curr = curr.Next
	}
	return head
}

/**
 * @Description: 打印链表
 * @param head 头结点
 */
func PrintList(head *ListNode) {
	for head != nil {
		fmt.Printf("%d ", head.Val)
		head = head.Next
	}
	fmt.Println()
}
